package ge.telasi.tasks.ui.user;

import ge.telasi.tasks.model.User;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.comp.TableModel;
import ge.telasi.tasks.ui.comp.TableUtils;
import ge.telasi.tasks.ui.log.Loggable;
import ge.telasi.tasks.ui.log.Logger;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;

/**
 * Displays relation to the user: assistants or bosses.
 *
 * @author dimitri
 */
public class UserTableBase extends JTable implements Loggable {
    private static final long serialVersionUID = -3287455492520705517L;
    static final int IMAGE = 0;
    static final int CODE = 1;
    static final int USERNAME = 2;
    static final int FULLNAME = 3;
    static final int STRUCTURE = 4;
    static final int WORK_PHONE = 5;

    private Logger logger;

    public UserTableBase() {
        setModel(new UserTableModel());
        decorateComlumns();
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() > 1) {
                    User user = getSelectedUser();
                    if (user != null) {
                        UserPropertiesDialog dialog = new UserPropertiesDialog(UserTableBase.this);
                        dialog.setUser(user);
                        UIUtils.openDialog(dialog);
                    }
                }
            }
        });
    }

    void decorateComlumns() {
        TableColumnModel colModel = getColumnModel();
        colModel.getColumn(IMAGE).setPreferredWidth(20);
        colModel.getColumn(IMAGE).setResizable(false);
        colModel.getColumn(CODE).setPreferredWidth(75);
        colModel.getColumn(USERNAME).setPreferredWidth(120);
        colModel.getColumn(FULLNAME).setPreferredWidth(150);
        colModel.getColumn(STRUCTURE).setPreferredWidth(300);
        colModel.getColumn(WORK_PHONE).setPreferredWidth(150);
    }

    @Override
    public Logger getLogger() {
        return logger;
    }

    @Override
    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    @Override
    public JComponent[] allFields() {
        return null;
    }

    public User getSelectedUser() {
        int index = getSelectedRow();
        if (index != -1) {
            return ((UserTableModel) getModel()).getObjectAt(index);
        }
        return null;
    }

    public List<User> getSelectedUsers() {
        int[] indecies = getSelectedRows();
        if (indecies != null && indecies.length > 0) {
            UserTableModel model = (UserTableModel) getModel();
            List<User> users = new ArrayList<User>();
            for (int index : indecies) {
                users.add(model.getObjectAt(index));
            }
            return users;
        }
        return null;
    }

    public void setSelectedUser(User user) {
        UserTableModel model = (UserTableModel) getModel();
        int index = model.indexOf(user);
        TableUtils.setSelectionAt(this, index);
    }

    public void display(List<User> users) {
        UserTableModel model = (UserTableModel) getModel();
        model.setData(users);
    }

    class UserTableModel extends TableModel<User> {
        private static final long serialVersionUID = -3810368036965776876L;
        @Override
        public int getColumnCount() {
            return 6;
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
                case IMAGE:
                    return "";
                case CODE:
                    return "კოდი";
                case USERNAME:
                    return "მომხმ. სახელი";
                case FULLNAME:
                    return "ნამდვილი სახელი";
                case STRUCTURE:
                    return "სტრუქტურული ერთეული";
                case WORK_PHONE:
                    return "სამს. ტელეფონი";
                default:
                    return "?";
            }
        }

        @Override
        public Class<?> getColumnClass(int column) {
            switch (column) {
                case IMAGE:
                    return Icon.class;
                case CODE:
                case USERNAME:
                case FULLNAME:
                case STRUCTURE:
                case WORK_PHONE:
                    return String.class;
                default:
                    return Object.class;
            }
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            switch (column) {
                default:
                    return false;
            }
        }

        @Override
        public Object getValueAt(int row, int column) {
            User user = getObjectAt(row);
            switch (column) {
                case IMAGE:
                    if (user.isActive() && !user.isNew()) {
                        return user.isAdmin() ? UIUtils.getIcon16x16("tux.png") : UIUtils.getIcon16x16("user.png");
                    } else if (!user.isActive()) {
                        return UIUtils.getIcon16x16("user_delete.png");
                    } else if (user.isNew()) {
                        return UIUtils.getIcon16x16("user_add.png");
                    }
                    return null;
                case CODE:
                    return user.getCode();
                case USERNAME:
                    return user.getUsername();
                case FULLNAME:
                    return user.getFullName();
                case STRUCTURE:
                    return user.getStructure().getName();
                case WORK_PHONE:
                    return user.getWorkPhone();
                default:
                    return null;
            }
        }

    }

}
